<?php

namespace App\Http\Controllers\Web;

use App\Caches\WanSdkStartQueryCache;
use App\Http\Controllers\Controller;
use App\Models\Channels;
use App\Models\Games;
use App\Services\GameChannelService;
use App\Services\GameHtService;
use App\Services\WanSdk\ToApkService;
use App\Services\WanSdk\WanSdkService;
use App\Structs\ToApkStruct;
use Illuminate\Http\Request;
use YQ\YqExtend;

class WanSdkToApkController extends Controller
{
    // 转端入口
    // 针对 2001小游戏转到2002H5微端
    // 或者 2002H5转到微端
    public function index(Request $request, $game_id, $game_channel_id)
    {
        $params       = $request->all();
        $channel      = Channels::CHANNEL_H5;
        $game_channel = GameChannelService::getInstance()->checkAndGet($game_id, $channel, $game_channel_id);

        // 根据实际的渠道应用，重新赋值channel
        $channel                   = $game_channel['channel'];
        $params['channel']         = $channel;
        $params['game_channel_id'] = $game_channel['id'];
        $game_channel_id           = $game_channel['id'];

        // 缓存外部透传的query
        $cache_id = YqExtend::uniqid32();
        WanSdkStartQueryCache::getInstance()->update($cache_id, $params);

        // 如果是微信，则唤起公众号登录
        $iswechat = WanSdkService::getInstance()->isWechatUa();
        if ($iswechat) {
            $cb_rui = 'h5_toapk/oauth';
            $scope = 'snsapi_base';
            if ($channel != Channels::CHANNEL_H5) {
                $scope = 'snsapi_userinfo';
            }
            $url = WanSdkService::getInstance()->getWechatOauthUrl($game_id, $game_channel_id, [
                'cache_id' => $cache_id,
            ], $cb_rui, $scope);
            return redirect()->away($url);
        }

        return view('h5_toapk.fail', [
            'message' => lang(6039),
        ]);
    }

    // 唤起转端页面
    // 通常情况下是在微信授权成功访问此接口的，也有可能ios用户点击右上角跳转到默认浏览器打开
    // 此时就需要基于 game_id 和 cache_id 的数据进行处理
    public function oauthCb(Request $request)
    {
        $game_id = $request->input('game_id');
        $cache_id = $request->input('cache_id') ?? '';

        $query = WanSdkStartQueryCache::getInstance()->get($cache_id);
        if (empty($query)) {
            $query = [];
        }
        if (!isset($query['device_type'])) {
            $query['device_type'] = WanSdkService::getInstance()->getPhoneType();
        }

        $game            = Games::where('game_id', $game_id)->first()->toArray();
        $channel         = $query['channel'] ?? Channels::CHANNEL_H5;
        $game_channel_id = $query['game_channel_id'] ?? 0;
        $game_channel    = GameChannelService::getInstance()->checkAndGet($game_id, $channel, $game_channel_id);

        // 根据实际的渠道应用，重新赋值channel
        $channel         = $game_channel['channel'];
        $game_channel_id = $game_channel['id'];

        // 公众号用户openid
        $openid = '';
        $unionid = '';
        $code = $request->input('code') ?? '';
        if (!empty($code)) {
            $info = WanSdkService::getInstance()->getWechatOffiaccountInfo($game_id, $game_channel_id, $code);
            if (!empty($info)) {
                $openid = $info['openid'];
                $unionid = $info['unionid'];
            }
        }

        // 如果授权失败，则判断code是否存在缓存中，如果是则认为玩家是通过刷新页面进来的
        if (empty($openid)) {
            $openid = $query['openid'] ?? '';
            $unionid = $query['unionid'] ?? '';
        } else {
            $query['openid'] = $openid;
            $query['unionid'] = $unionid;
            WanSdkStartQueryCache::getInstance()->update($cache_id, $query);
        }

        if (empty($openid)) {
            return view('h5_toapk.fail', [
                'message' => lang(6040),
            ]);
        }

        // 是否微信
        $is_wechat = WanSdkService::getInstance()->isWechatUa()==true?1:0;
        // 是否手机端
        $is_phone = WanSdkService::getInstance()->isPhone()==true?1:0;
        $device_type = WanSdkService::getInstance()->getPhoneType();

        // 根据不同渠道的转端，进行处理
        // 直接返回处理结果，如展示view 或者跳转页面等
        $struct = new ToApkStruct;
        $struct->isWechat = $is_wechat;
        $struct->isPhone = $is_phone;
        $struct->deviceType = $device_type;
        $struct->params = $query;

        $class = ToApkService::getInstance()->loadSDK($game_channel);
        return $class->handle($struct);
    }
}
